import device from '@system.device'

var deviceData = {
  getInfo: null,
  getId: null,
  getUserId: null,
  getOAID: null,
  getPlatform: null,
  getHost: null,
}
/**
 * 获取设备信息
 * @returns 返回设备信息
 */
function getInfo() {
  return new Promise((resolve, reject) => {
    if (deviceData.getInfo) {
      resolve(deviceData.getInfo)
    } else {
      device.getInfo({
        success: (res) => {
          deviceData.getInfo = res
          resolve(res)
        },
        fail: (err) => {
          reject(err)
        },
      })
    }
  })
}
/**
 * 批量获取设备标识，需要用户授权
 * @param {Array} typeList 支持 device、mac、user、advertising 1000+四种类型，可提供一至多个
 * @returns 设备标识
 */
function getId(type = ['device']) {
  return new Promise((resolve, reject) => {
    if (deviceData.getId) {
      resolve(deviceData.getId)
    } else {
      device.getId({
        type,
        success: (res) => {
          deviceData.getId = res
          resolve(res)
        },
        fail: (err) => {
          reject(err)
        },
      })
    }
  })
}
/**
 * 获取用户唯一标识
 * @returns 用户唯一标识
 */
function getUserId() {
  return new Promise((resolve, reject) => {
    if (deviceData.getUserId) {
      resolve(deviceData.getUserId)
    } else {
      device.getUserId({
        success: (res) => {
          deviceData.getUserId = res
          resolve(res)
        },
        fail: (err) => {
          reject(err)
        },
      })
    }
  })
}
/**
 * 返回厂商设备标识符中的 OAID（匿名设备标识符）
 * @returns 厂商设备标识符中的 OAID
 */
function getOAID() {
  return new Promise((resolve, reject) => {
    if (deviceData.getOAID) {
      resolve(deviceData.getOAID)
    } else {
      device.getOAID({
        success: (res) => {
          deviceData.getOAID = res
          resolve(res)
        },
        fail: (err) => {
          reject(err)
        },
      })
    }
  })
}
/**
 * 同步方法获取平台版本信息
 */
function getPlatform() {
  if (deviceData.getPlatform) {
    return deviceData.getPlatform
  } else {
    deviceData.getPlatform = device.platform
    return deviceData.getPlatform
  }
}
function getHost() {
  if (deviceData.getHost) {
    return deviceData.getHost
  } else {
    deviceData.getHost = device.host
    return deviceData.getHost
  }
}
export default {
  getInfo,
  getId,
  getUserId,
  getOAID,
  getPlatform,
  getHost,
}